package test.gen.core;

import gen.core.Generator;
import gen.core.utils.GenUtils;

import java.sql.Connection;
import java.util.Properties;

import junit.framework.TestCase;

public class GeneratorTestCase extends TestCase {
	private Generator generator;
	
	public void setUp(){
		generator = new Generator();
		generator.init();
	}
	
	public void testGenByArrays() {
		String[][] arrays = { { "abc", "def" }, { "123", "456" } };

		String templateString = "#foreach($array in $Arrays)$array[0] $array[1]\n#end";
		String result = generator.genByArrays(arrays, templateString);

		assertEquals("abc def\n123 456\n", result);
	}

	public void testGenBySql() throws Exception {
		String aDatabaseFile = "test.db";
		String sql = "select * from gen_template";
		String aTemplateString = "#foreach($row in $Table.Rows)INSERT INTO $Table.Name VALUES($StringUtils.join($row.Cells, \", \"));\n#end";

		Properties aProperties = new Properties();
		aProperties.setProperty("driver", "org.sqlite.JDBC");
		aProperties.setProperty("url", "jdbc:sqlite:" + aDatabaseFile);

		Connection conn = null;
		try {
			conn = GenUtils.createConnection(aProperties);
			String result = generator.genBySql(conn, sql, aTemplateString);
			System.out.println(result);
		} finally {
			GenUtils.closeConnection(conn);
		}

	}
}
